<template>
  <el-form
    ref="ruleForm"
    label-width="120px"
    style="width: 40%"
    :model="formLabelAlign"
    :rules="rules"
  >
    <el-form-item label="卡号类型" prop="type">
      <el-radio-group
        v-model="formLabelAlign.type"
        size="small"
        @change="changeType"
      >
        <el-radio-button :label="0"> 对私 </el-radio-button>
        <el-radio-button :label="1"> 对公 </el-radio-button>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="账户名" prop="accountName">
      <el-input v-model="formLabelAlign.accountName"></el-input>
    </el-form-item>
    <el-form-item label="开户行" prop="bankName">
      <el-input v-model="formLabelAlign.bankName"></el-input>
    </el-form-item>
    <el-form-item label="开户支行" prop="bankSubName">
      <el-input v-model="formLabelAlign.bankSubName"></el-input>
    </el-form-item>
    <el-form-item label="银行账号" prop="cardNo">
      <el-input v-model="formLabelAlign.cardNo"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm('ruleForm')">
        提交
      </el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { getBankCard, saveBankCard } from "@/api/user";
import { Message } from "element-ui";
export default {
  name: "BillInfo",
  data() {
    return {
      formLabelAlign: {
        accountName: null,
        bankName: null,
        bankSubName: null,
        cardNo: null,
        id: null,
        type: 0,
      },
      rules: {
        accountName: [
          { required: true, message: "账户名不能为空", trigger: "blur" },
        ],
        bankName: [
          { required: true, message: "开户行不能为空", trigger: "blur" },
        ],
        bankSubName: [
          { required: true, message: "开户支行不能为空", trigger: "blur" },
        ],
        cardNo: [
          { required: true, message: "银行账号不能为空", trigger: "blur" },
        ],
      },
    };
  },
  mounted() {
    this.getBankCard(this.formLabelAlign.type);
  },
  methods: {
    changeType(param) {
      this.formLabelAlign.type = param;
      this.getBankCard(param);
    },
    getBankCard(type) {
      getBankCard(type).then((res) => {
        this.formLabelAlign = res.data;
        this.formLabelAlign.type = type;
      });
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          saveBankCard(this.formLabelAlign).then(() => {
            Message({
              message: "提交成功！",
              type: "success",
              duration: 5 * 1000,
            });
            this.getBankCard(this.formLabelAlign.type);
          });
        } else {
          return false;
        }
      });
    },
  },
};
</script>

<style scoped lang="scss">
::v-deep .el-radio-button--small .el-radio-button__inner {
  padding: 10px 20px;
  font-size: 14px;
  width: 100px;
}
</style>
